package logic.file;

import java.awt.Desktop;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Time;
import java.util.Date;
import java.util.zip.ZipOutputStream;

import org.apache.log4j.Logger;

import logic.FileHelper;
import logic.UnsupportedProtocol;
import logic.operation.ByteCounter;

public class LocalFile implements IFile {
	
	static Logger logger = Logger.getLogger(LocalFile.class.getName());
	
	File file;
	
	public LocalFile(String path)  {
		file = new File(path);
	}

	@Override
	public String getName() {
		return file.getName();
	}

	@Override
	public String getPath() {
		return file.getPath();
	}

	@Override
	public String getType() {
		if (isDirectory()) {
			return "<DIR>";
		}
		return FileHelper.getExtension(file);
	}
	
	//TODO cellrenderer niech przelicza do human readable
	@Override
	public long getSize() {
		return file.length();
	}

	@Override
	public void delete() {
		file.delete();		//usuwa tylko pliki lub PUSTE foldery
	}
	
	public String toString() {				//toString przedefiniowany w IDirectory
		return file.toString();
	}

	//TODO prawdopodobnie konieczne wywoływanie metod natywnych, ew. metody z klasy Files
	@Override
	public String getFilePermissions() {
		return "not support. yet";
	}

	@Override
	public long getLastModifiedDate() {
		//TODO
		return file.lastModified();
	}

	@Override
	public boolean isDirectory() {
		return file.isDirectory();
	}

	public long calcDirSize() throws UnsupportedProtocol {
		// TODO Auto-generated method stub
		return 0;
	}
	
	@Override
	public void copyTo(String path) throws IOException {
		this.copyTo(path, null);
	}
	
	//IDirectory będzie bezpieczniejsze i lepsze
	/**
	 * 
	 * @param path ścieżka do folderu docelowego
	 * @throws IOException 
	 */
	@Override
	public void copyTo(String path, ByteCounter bc) throws IOException {
		//TODO czytanie po buforze o jakims rozmiarze i zapisywanie
		logger.debug("w File.copyTo, path:" + path);
		
		bc.setCurrentFile(file.getName());
		
		InputStream input = new FileInputStream(file);
		OutputStream output = new FileOutputStream(new File(path + System.getProperty("file.separator") + file.getName()));
		byte [] buf = new byte[1024];
		int bRead = 0;
		while ((bRead = input.read(buf)) != -1) {
			output.write(buf, 0, bRead);
			if (bc != null) {
				bc.add(bRead);
			}
		}
		input.close();
		output.close();
	}

	@Override
	public void open() throws IOException {
		Desktop.getDesktop().open(file);
		
	}
	
	@Override
	public void compress(IFile directory) throws IOException {
		InputStream input = new FileInputStream(file);
		ZipOutputStream output = new ZipOutputStream(new FileOutputStream(directory.getPath()));
		//TODO output.putEntry
		byte [] buf = new byte[1024];
		while (input.read(buf) != -1) {
			output.write(buf);
		}
		input.close();
		output.close();
	}

}
